package me.zk.practice1.hw;

import java.util.*;

/**
 * @author zhangkai
 * @date 2023/8/11 18:27
 */
public class HWBJJ07 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Node> list = new ArrayList<>();
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            if (s.equals("1")) {
                break;
            }
            int[] nums = Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();

            list.add(new Node(nums[0],nums[1]));


        }
        Collections.sort(list);
        System.out.println();
        List<Node> res = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Node n1 = list.get(i);
            for (int j = i+1; j < list.size(); j++) {
                Node n2 = list.get(j);
                if (n1.end >= n2.start) {
                    int l = Math.max(n1.start,n2.start);
                    int r = Math.min(n1.end,n2.end);
                    res.add(new Node(l,r));
                }
            }
        }
        if (res.isEmpty()) {
            System.out.println("None");
            return;
        }
        Collections.sort(res);
        Node node = res.get(0);
        int s = node.start;
        int e = node.end;
        for (Node n :res) {
            if (n.start > e) {
                System.out.println(s + " " + e);
                s = n.start;
                e = n.end;
            } else {
                s = Math.min(s,n.start);
                e = Math.max(e,n.end);
            }

        }
        System.out.println(s + " " + e);
    }
    static class Node implements Comparable<Node> {
        int start;
        int end;

        public Node(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public int compareTo(Node node) {
            if (node.start == this.start) {
                return this.end - node.end;
            }
            return this.start- node.start;
        }
    }
}

